package com.wmh.baseservice.merchant.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.common.annotation.parameterverify.ParameterVerify;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.pojo.SortWay;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.merchant.pojo.MerchantInfo;
import com.wmh.baseservice.merchant.pojo.MerchantPlotRegion;
import com.wmh.baseservice.merchant.pojo.MerchantPlotRentalMenu;
import com.wmh.baseservice.merchant.pojo.MerchantVegetablePlot;
import com.wmh.baseservice.merchant.service.MerchantInfoService;
import com.wmh.baseservice.merchant.service.MerchantPlotRegionService;
import com.wmh.baseservice.merchant.service.MerchantVegetablePlotService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.wmh.baseservice.merchant.service.MerchantPlotRentalMenuService;

/**
 * @author wmh generator
 * @since 2022-01-18
 */
@Api(tags = "商家端-土地区域租赁套餐表")
@RestController
@RequestMapping(value = "merchant/merchantPlotRentalMenu", produces = "text/plain;charset=utf-8")
@RequiresPermissions(logical = Logical.OR, value = {"租赁套餐管理操作", "所有权限"})
@RequiresRoles("merchant")

public class MerchantPlotRentalMenuController {

    @Resource
    private MerchantPlotRentalMenuService merchantPlotRentalMenuService;
    @Resource
    private MerchantPlotRegionService merchantPlotRegionService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private MerchantVegetablePlotService merchantVegetablePlotService;

    @ApiOperation("列表")
    @PostMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id ", value = "id", required = false),
    })
    public String list(PageInfo pageInfo) {
        Long id = (Long) request.getAttribute("id");
        pageInfo.setTimeScreen("create_time");
        pageInfo.setDefaultSort("create_time", SortWay.DESC);
        MerchantInfo merchantInfo = merchantInfoService.getByIdForUpdate(id);
        if (merchantInfo.getParentId() != -1) {
            MerchantInfo parentMerchantInfo = merchantInfoService.getOneForUpdate(new QueryWrapper<MerchantInfo>().eq(MerchantInfo.US_ID, merchantInfo.getParentId()));
            id = parentMerchantInfo.getId();
        }
        //执行分页辅助工具
        Long finalId = id;
        PageResult<MerchantPlotRentalMenu> pageResult = new PageUtil<MerchantPlotRentalMenu>(pageInfo).startPage((page, queryWrapper) -> {
            queryWrapper.eq(MerchantPlotRentalMenu.MERCHANT_ID, finalId);
            merchantPlotRentalMenuService.page(page, queryWrapper);
        });
        pageResult.getList().forEach(item -> {
            MerchantPlotRegion merchantPlotRegion = merchantPlotRegionService.getByIdForUpdate(item.getRegionId());
            if (CommUtil.checkNull(merchantPlotRegion)) {
                item.setRegionName(merchantPlotRegion.getName());
            }
        });
        return ReturnBody.success(pageResult);
    }

    @ApiOperation("添加或修改")
    @PostMapping("addOrUpdate")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String addOrUpdate(MerchantPlotRentalMenu merchantPlotRentMenu) {
        Long id = (Long) request.getAttribute("id");
        Long regionId = merchantPlotRentMenu.getRegionId();
        merchantPlotRentMenu.setMerchantId(id);
        //只有拥有该土地的才可以进行添加或修改
        MerchantVegetablePlot merchantVegetablePlot = merchantVegetablePlotService.getOneForUpdate(new QueryWrapper<MerchantVegetablePlot>().eq(MerchantVegetablePlot.RAW_MERCHANT_ID, id));
        if (!CommUtil.checkNull(merchantVegetablePlot)) {
            return ReturnBody.error("无权操作，因为您未拥有土地");
        }
        MerchantPlotRegion merchantPlotRegion = merchantPlotRegionService.getByIdForUpdate(regionId);
        if (!CommUtil.checkNull(merchantPlotRegion)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        if (CommUtil.checkNull(merchantPlotRentMenu.getId())) {
            if (!merchantPlotRegion.getMerchantId().equals(merchantVegetablePlot.getRawMerchantId())) {
                return ReturnBody.error("无权操作，因为您不是该区域所属土地原商家");
            }
        }
        if (!merchantPlotRentalMenuService.saveOrUpdate(merchantPlotRentMenu)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        return ReturnBody.success();
    }

    @ApiOperation("删除")
    @PostMapping("delete")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    @ParameterVerify(notNull = "id")
    public String delete(Long id) {
        Long merchantId = (Long) request.getAttribute("id");
        MerchantPlotRentalMenu merchantPlotRentalMenu = merchantPlotRentalMenuService.getByIdForUpdate(id);
        if (!CommUtil.checkNull(merchantPlotRentalMenu)) {
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        //只有拥有该土地的才可以进行添加或修改
        MerchantVegetablePlot merchantVegetablePlot = merchantVegetablePlotService.getOneForUpdate(new QueryWrapper<MerchantVegetablePlot>().eq(MerchantVegetablePlot.RAW_MERCHANT_ID, merchantId));
        if (!CommUtil.checkNull(merchantVegetablePlot)) {
            return ReturnBody.error("无权操作，因为您未拥有土地");
        }

        if (!merchantPlotRentalMenu.getMerchantId().equals(merchantVegetablePlot.getRawMerchantId())) {
            return ReturnBody.error("无权操作，因为您不是该区域所属土地原商家");
        }

        if (!merchantPlotRentalMenuService.removeById(id)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        return ReturnBody.success();
    }


}
